<template>

    <a-form-model ref="form" :rules="rules" :model="form"  layout="vertical" class="rx-a-form-vertical">
        <a-form-model-item :label="i18n('form.formScheme','表单方案')" prop="form" >
            <rx-input-button   @click="selectForm" v-model="form.form" ></rx-input-button>
        </a-form-model-item>

        <a-form-model-item :label="i18n('form.dataMap','数据映射')"   >
            <a-radio-group v-model="form.mapType" :options="mapTypes"></a-radio-group>

        </a-form-model-item>
        <a-form-model-item :label="i18n('form.primaryKey','主键')" prop="pkField" v-if="form.mapType=='pk'" >
            <a-select   allowClear v-model="form.pkField"  :options="localFields"/>
        </a-form-model-item>
        <a-form-model-item :label="i18n('form.colMap','列映射')" v-if="form.mapType=='noPk'">
            <a-table
                :columns="columns"
                :dataSource="form.fieldMap"
                :pagination="false"
                :bordered="true"
                rowKey="idx_"

            >
                <template slot="destField" slot-scope="text, record, index"  >
                   {{text}}
                </template>

                <template slot="srcField" slot-scope="text, record, index"  >
                    <a-select
                              v-model="record.srcField" :options="localFields"/>
                </template>

            </a-table>
        </a-form-model-item>

    </a-form-model>

</template>

<script>

/**
 * {
 *  //表单别名
 *  alias:"",
 *  //表单名称
 *  name:"",
 *  {text:"",value:""}
 *  form:""
 *  //是否只读
 *  mapType:"pk,noPk"
 *  //主键
 *  pkField:"",
 *  //字段映射
 *  fieldMap: [{ destField: "",srcField: "", idx_: 0}]
 * }
 */

import {BaseFormModel,GridMixin} from "@lowcode/jpaas-common-lib";
import DialogBox from "@/assets/js/DialogBox";
import linkBase from "@/views/modules/form/core/cellRender/linkBase";

export default {
    name: "link-form-component",
    props:["layerid","destroy","config","fields"],
    mixins: [BaseFormModel,GridMixin,linkBase],
    data(){
        return {
            labelCol: {span: 6},
            wrapperCol: {span: 18},
            idx_: 1,
            form:{
                alias:"",
                name:"",
                form:"",
                mapType:"pk",
                pkField:"",
                fieldMap: [],
            },
            columns: [
                {
                    title: this.i18n('form.targetField','目标字段'),
                    dataIndex: 'destField',
                    scopedSlots: {customRender: 'destField'},
                },
                {
                    title: this.i18n('form.listField','当前列表字段'),
                    dataIndex: 'srcField',
                    scopedSlots: {customRender: 'srcField'}
                }
            ],

            localFields:[],
            selectedRowKeys: [],
            selectedRows: [],
            mapTypes:[{label:this.i18n('form.primaryKey','主键'),value:"pk"},{label:this.i18n('form.noKey','无主键'),value:"noPk"}],
            rules: {
                form: [
                    {required: true, message: this.i18n('msa.formNeed','表单必填'), trigger: 'change'},
                ],
            }
        }
    },
    created() {
        this.init();
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"formCellRender",key);
        },
        init(){
            if(this.config){
                let obj=this.parse(this.config);
                if(obj.hasOwnProperty("alias")){
                    this.form=obj;
                }
            }
        },
        getData(){
            return this.form;
        },
        selectForm(vm){
            var self=this;
            var config = {curVm: self, widthHeight: ['800px', '600px'], data: {multiSelect: false}};
            DialogBox.openSoution(config, function (data) {
                self.form.alias=data.alias;
                self.form.name=data.name;
                self.form.form=JSON.stringify({text:data.name,value:data.alias})
                self.handNoPkSetting(data);
            })
        },
        handNoPkSetting(data){
            this.destFields=[];
            if(!data.noPkSetting){
                return;
            }
            let noPkSetting = JSON.parse(data.noPkSetting);
            for(let j=0;j<noPkSetting.length;j++){
                let item =  noPkSetting[j];
                if(item.valueSource!="param"){
                    continue;
                }
                this.form.fieldMap.push({destField:item.name,srcField:""});
            }
        },
        valid(){
            return new Promise((resolve,reject)=>{
                this.$refs.form.validate(valid => {
                    if(valid){
                        resolve(true)
                    }
                    else{
                        reject(false);
                    }
                });
            })
        }


    },
    watch:{
        fields:{
            handler(val,oldVal){
                this.localFields=val;
            },
            deep : true,
            immediate:true
        }
    }


}
</script>



